﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UDataBaseWork;
using XF.Meeting.Entity.dbEntity;
using XF.Meeting.Entity.DTO;
using XF.Meeting.Entity.Query;
using XF.Meeting.IDal;

namespace XF.Meeting.DAL
{
    public class UserDAL : BaseDAL, IUserDAL
    {
        public Task<Tuple<DataTable,string>> Login(string UserName, string UserPassword)
        {
            string strError = "";
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@UserName",UserName),
                  new SqlParameter("@UserPassword",UserPassword),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetUserInfo",out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetUserInfo EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;

            }
            var res = new Tuple<DataTable, string>(ds.Tables[0], strError);
            return Task.FromResult(res);
        }

        public Task<Tuple<DataTable, string>> Login(string phone)
        {
            string strError = "";
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@phone",phone),
                 
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetUserInfoByPhone", out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetUserInfoByPhone EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;

            }
            var res = new Tuple<DataTable, string>(ds.Tables[0], strError);
            return Task.FromResult(res);
        }

        public Task<Tuple<bool, string>> UpdatePassword(string username, string password)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<(bool,int)> InsertUserInfo(UserInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@RID",entity.RegionalID),
                  new SqlParameter("@Account",entity.Account),
                  new SqlParameter("@Password",entity.Password),
                  new SqlParameter("@AccountName",entity.AccountName),
                  new SqlParameter("@Position",entity.Position),
                  new SqlParameter("@Phone",entity.Phone),
                  new SqlParameter("@Mail",entity.Mail),
                  new SqlParameter("@IsAdmin",entity.IsAdmin),
                  new SqlParameter("@IsEnable",entity.IsEnable),
                  new SqlParameter("@Integral",entity.Integral),

                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spInsertUserInfo", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spInsertUserInfo EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult((bResult, ReturnValue));
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> DeleteUserInfoByID(UserInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@ID",entity.ID),
                 
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spDeleteUserInfo", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spDeleteUserInfo EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }

        /// <summary>
        /// 根据ID获取用户信息
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<Tuple<DataTable, DataTable, string>> GetUserInfoByID(UserQuery query, out string strError)
        {
          
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@ID",query.ID),

                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetUserInfoByID", out DataSet ds, out strError))
            {
                
                strError = string.Format("DBProvider spGetUserInfoByID EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            var res = new Tuple<DataTable, DataTable,string>(ds.Tables[0], ds.Tables[1],strError);
            return Task.FromResult(res);
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> UpdateUserInfo(UserInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@ID",entity.ID),
                  new SqlParameter("@RID",entity.RegionalID),
                  new SqlParameter("@Password",entity.Password),
                  new SqlParameter("@AccountName",entity.AccountName),
                  new SqlParameter("@Position",entity.Position),
                  new SqlParameter("@Phone",entity.Phone),
                  new SqlParameter("@Mail",entity.Mail),
                  new SqlParameter("@IsAdmin",entity.IsAdmin),
                  new SqlParameter("@IsEnable",entity.IsEnable),
                  new SqlParameter("@Integral",entity.Integral),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spUpdateUserInfo", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spUpdateUserInfo EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
        public Task<bool> DeleteUserOrgForOrgcalendarsAuth(UserInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@UserID",entity.ID),
                  
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spDeleteUserOrgForOrgcalendarsAuth", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spDeleteUserOrgForOrgcalendarsAuth EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
        public Task<bool> InsertUserOrgForOrgcalendarsAuth(UserInfo entity, out string strError)
        {
            bool bResult = true;
            strError = string.Empty;
            foreach (var item in entity.OrgIDs)
            {
                SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@UserID",entity.ID),
                  new SqlParameter("@OrgID",item),
                };
                CParameters paras = new CParameters();
                paras.AddRange(sqlParameters);
                if (!_db.ExcuteByTransaction(paras, "spInsertUserOrgForOrgcalendarsAuth", out int ReturnValue, out  strError))
                {
                    bResult = false;
                    strError = string.Format("DBProvider spInsertUserOrgForOrgcalendarsAuth EXECUTE_DB Error:{0}", strError);
                }
                else
                {
                    strError = string.Empty;
                }
            }
            
            return Task.FromResult(bResult);
        }

        /// <summary>
        /// 根据区域ID获取当前区域下所有人员信息
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<Tuple<DataTable, int, int>> GetUserInfoListByRID(UserQuery query, out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@RID",query.RID),
                  new SqlParameter("@Key",query.Key),
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetUserInfoByRID", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetUserInfoByRID EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            var tup = new Tuple<DataTable, int, int>(ds.Tables[0], TotalRowsCount, TotalPageCount);
            return Task.FromResult(tup);
        }

        /// <summary>
        /// 根据查询关键字Key获取所有人员信息
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<Tuple<DataTable, int, int,bool>> GetUserInfoListByKey(UserQuery query, out string strError)
        {
            bool IsHaveNext = false;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@Key",query.Key),
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetUserInfoByKey", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetUserInfoByKey EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;

            }
            if (TotalRowsCount > query.pageIndex * query.pageSize)
            {
                IsHaveNext = false;
            }
            else
            {
                IsHaveNext = true;
            }
            var tup = new Tuple<DataTable, int, int,bool>(ds.Tables[0], TotalRowsCount, TotalPageCount, IsHaveNext);
            return Task.FromResult(tup);
        }

        /// <summary>
        /// 根据查询关键字Key获取所有人员信息
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<Tuple<DataTable, int, int>> GetUserInfoList(UserQuery query, out string strError)
        {
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@Key",query.Key),
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetUserInfoList", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetUserInfoList EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            var tup = new Tuple<DataTable, int, int>(ds.Tables[0], TotalRowsCount, TotalPageCount);
            return Task.FromResult(tup);
        }

        /// <summary>
        /// 根据用户ID获取用户积分
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<Tuple<DataTable, string>> GetUserIntegral(UserQuery query, out string strError)
        {

            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@UserID",query.ID),

                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spCheckUserIntegral", out DataSet ds, out strError))
            {

                strError = string.Format("DBProvider spCheckUserIntegral EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            var res = new Tuple<DataTable, string>(ds.Tables[0], strError);
            return Task.FromResult(res);
        }

        /// <summary>
        /// 编辑用户积分
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> UpdateUserIntegral(UserIntegral entity,out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@UserID",entity.UserID),
                 
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spUpdateUserIntegral", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spUpdateUserIntegral EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }

        /// <summary>
        /// 修改用户区域
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> UpdateUserRegional(UserInfo entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                   new SqlParameter("@UserID",entity.ID),
                   new SqlParameter("@RegionalID",entity.RegionalID),

                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spUpdateUserRegional", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spUpdateUserRegional EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }

        /// <summary>
        /// 获取白名单用户
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<Tuple<DataTable, int, int, bool>> GetWhiteList(UserQuery query, out string strError)
        {
            bool IsHaveNext = false;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                  new SqlParameter("@Key",query.Key),
                  new SqlParameter("@PageIndex",query.pageIndex),
                  new SqlParameter("@PageSize",query.pageSize),
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spGetWhiteList", out int TotalRowsCount, out int TotalPageCount, out DataSet ds, out strError))
            {
                strError = string.Format("DBProvider spGetWhiteList EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;

            }
            if (TotalRowsCount > query.pageIndex * query.pageSize)
            {
                IsHaveNext = false;
            }
            else
            {
                IsHaveNext = true;
            }
            var tup = new Tuple<DataTable, int, int, bool>(ds.Tables[0], TotalRowsCount, TotalPageCount, IsHaveNext);
            return Task.FromResult(tup);
        }

        /// <summary>
        /// 新增白名单用户
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> InsertUserToWhiteList(int ID, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                   new SqlParameter("@UserID",ID),
                   

                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spInsertUserToWhiteList", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spInsertUserToWhiteList EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
        /// <summary>
        /// 修改白名单用户企业微信通知
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> UpdateToWhiteListUserMsg(WhiteListUser entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                   new SqlParameter("@ID",entity.ID),
                   new SqlParameter("@IsSendWechatMessage",entity.IsSendWechatMessage),
                   


                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spUpdateToWhiteListUserMsg", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spUpdateToWhiteListUser EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
        /// <summary>
        /// 修改白名单用户企业日程推送
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> UpdateToWhiteListUserSchedule(WhiteListUser entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                   new SqlParameter("@ID",entity.ID),
                   
                   new SqlParameter("@IsSendWechatSchedule",entity.IsSendWechatSchedule),


                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spUpdateToWhiteListUserSchedule", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spUpdateToWhiteListUserSchedule EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
        /// <summary>
        /// 删除白名单用户
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public Task<bool> DeleteToWhiteListUser(WhiteListUser entity, out string strError)
        {
            bool bResult = true;
            SqlParameter[] sqlParameters = new SqlParameter[] {
                   new SqlParameter("@ID",entity.ID),
                 
                };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            if (!_db.ExcuteByTransaction(paras, "spDeleteToWhiteListUser", out int ReturnValue, out strError))
            {
                bResult = false;
                strError = string.Format("DBProvider spDeleteToWhiteListUser EXECUTE_DB Error:{0}", strError);
            }
            else
            {
                strError = string.Empty;
            }
            return Task.FromResult(bResult);
        }
    }
}
